package java6699.lesson14;

import java.io.*;
import java.net.Socket;
import java.util.Properties;
import java.util.Scanner;

/**
 * @program: java_zzx
 * @description:
 * @author: zzx
 * @create: 2022-12-07 19:12
 **/
public class OneClient6699 {

    private int port;
    private String host;

    public OneClient6699() {
        Properties properties = new Properties();
        try {
            String path = new File("config/oneClient6699.properties").getAbsolutePath();
            int i = path.indexOf("Java6699");
            FileReader fileReader = new FileReader(new File(path.substring(0,i+9)
                    +"config/oneClient6699.properties").getAbsolutePath());
            properties.load(fileReader);
        } catch (IOException e) {
            e.printStackTrace();
        }

        port = Integer.parseInt(properties.getProperty("port"));
        host = properties.getProperty("ip");
    }

    public void start() {
        System.out.println(port + "服务平台");
        try (Socket socket = new Socket(host, port)) {
            session(socket);//开始与客户端通话
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void session(Socket socket) throws IOException {
        // 包装Socket中的IO字节流
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintStream printer = new PrintStream(socket.getOutputStream());

        Scanner input = new Scanner(System.in);// 用Scanner实现键盘输入
        int count = 1;
        String problem = null;
        System.out.println(reader.readLine());
        while (true) {// 接收客户端的信息
            input = new Scanner(System.in);
            System.out.print("请输入问题：");
            String question = input.nextLine();
            printer.println(question);
            System.out.println("回答" + count++ + "：" + reader.readLine());
            if ("谢谢".equals(question.trim())) {
                break;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new OneClient6699().start();
    }
}
